﻿using System;
using System.Collections.Generic;

using Moe.ECamel.Common.Entities;
using Moe.ECamel.Common.Utils;
using Moe.ECamel.Client.ServiceProxies;

namespace Moe.ECamel.Client.Controllers
{
	public class SubscriptionsController : EntityController<Subscription>
	{
		public SubscriptionsController(int customerId)
			: base(customerId)
		{
		}

		public override Subscription NewEntity()
		{
			return NewSubscription(ECamelHelper.SubscriptionTypes[1]);
		}

		public Subscription NewSubscription(SubscriptionType subscriptionType)
		{
			var subscription = base.NewEntity();
			subscription.CustomerId = this.ParentEntityId;
			subscription.InitializeNewSubscription(subscriptionType, SystemContainer.Clock.Today);
			return subscription;
		}

		public override ECamelError Create(Subscription entity)
		{
			using (var serviceProxy = new SubscriptionServiceProxy())
			{
				return serviceProxy.CreateSubscription(entity);
			}
		}

		public ECamelError UpdateSubscriptionStatus(Subscription subscription, bool active)
		{
			using (var serviceProxy = new SubscriptionServiceProxy())
			{
				return serviceProxy.UpdateSubscriptionStatus(subscription.SubscriptionId, active);
			}
		}

		public ECamelError Renew(Subscription subscription, DateTime renewalDate)
		{
			using (var serviceProxy = new SubscriptionServiceProxy())
			{
				return serviceProxy.RenewSubscription(subscription.SubscriptionId, renewalDate);
			}
		}

		protected override IList<Subscription> LoadList()
		{
			using (var serviceProxy = new SubscriptionServiceProxy())
			{
				return serviceProxy.GetSubscriptionsByCustomer(this.ParentEntityId);
			}
		}
	}
}